//
//  MenuPersonViewCell.swift
//  ams
//
//  Created by yangyuan on 2017/7/5.
//  Copyright © 2017年 coterjiesen. All rights reserved.
//

import UIKit

class PersonInfoview: UIView{
    let lablePersonTile : UILabel = {
        let lable = UILabel()
        lable.font = v2Font(15)
        lable.text = "名字："
        return lable
    }()
    
    let lablePersonValue : UILabel = {
        let lable = UILabel()
        lable.font = v2Font(14)
        lable.text = "--"
        return lable
    }()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        self.addSubview(lablePersonTile)
        self.addSubview(lablePersonValue)
        setupLayout()
    }
    func setupLayout(){
        self.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalTo(self)
        }

        lablePersonTile.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(8)
            make.centerY.equalTo(self)
        }
        lablePersonValue.snp.makeConstraints { (make) in
            make.left.equalTo(lablePersonTile.snp.right).offset(6)
            make.top.equalTo(lablePersonTile)
        }

    }
}

class PersonInfoViewCell: UITableViewCell{
    
    var content = PersonInfoview()
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    required override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    func setupUI(){
        self.isUserInteractionEnabled = false
        self.backgroundColor = UIColor.clear
        self.selectedBackgroundView?.backgroundColor = UIColor.clear
        self.selectionStyle = .none
        self.layer.cornerRadius = 20
        self.contentView.addSubview(content)
        content.snp.makeConstraints { (make) in
            make.top.equalTo(self.contentView).offset(2)
            make.left.right.bottom.equalTo(self.contentView)
        }
    }
    func bind(data: String){
        content.lablePersonValue.text = data
    }
    
}
class PersonInfoListView : UIView{
    
    var data :DataModel? = nil
    fileprivate var _tableView :UITableView!
    var tableView :UITableView{
        get{
            if (nil != _tableView) {
                return _tableView!;
            }
            _tableView = UITableView()
            //注册cell
            regClass(_tableView, cell: PersonInfoViewCell.self)
            _tableView.delegate = self
            _tableView.dataSource = self
            
            return _tableView!;
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        let closeView = UIView()
        self.backgroundColor = .green
        let closeImageView = UIImageView(image: UIImage(named:"ic_close"))
        let dragImageView = UIImageView(image: UIImage(named:"ic_drag"))
        self.addSubview(tableView)
        self.addSubview(closeView)
        closeView.addSubview(closeImageView)
        closeView.addSubview(dragImageView)
        //添加 拖拽和收起图标
        closeImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(closeView)
            make.centerX.equalTo(closeView).offset(-SCREEN_WIDTH / 4)
        }
        dragImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(closeView)
            make.centerX.equalTo(closeView).offset( SCREEN_WIDTH / 4)
        }
        
        closeView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.height.equalTo(40)
            make.bottom.equalTo(self)
        }
        //view中就包含这个tableview
        tableView.snp.makeConstraints { (make) in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(closeView.snp.top)
        }
    }
}

extension PersonInfoListView:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let count = self.data?.mangerList?.count{
            return count
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 30
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = getCell(tableView, cell: PersonInfoViewCell.self, indexPath: indexPath);
        cell.bind(data: (data?.mangerList?[indexPath.row])!)
        return cell;
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}

class MenuPersonView: UIView {
    let lableNum: UILabel = {
        let lable = UILabel()
        lable.font = v2Font(40)
        lable.textColor = .white
        lable.text = "0"
        return lable
    }()
    let lableTitle: UILabel = {
        let lable = UILabel()
        lable.font = v2Font(15)
        lable.text = "装维人员"
        lable.textColor = .white
        return lable
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        self.addSubview(lableNum)
        self.addSubview(lableTitle)
        self.snp.makeConstraints { (make) in
            make.left.top.right.bottom.equalTo(self)
        }
        self.lableNum.snp.makeConstraints { (make) in
            make.centerY.equalTo(self).offset(-15)
            make.centerX.equalTo(self)
        }
        self.lableTitle.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(38)
        }
    }
}

class MenuPersonInfoViewCell: FoldingCell{
    //展开时的视图
    let _containerView = Init(PersonInfoListView(frame: .zero)) {
        $0.backgroundColor = .green
        $0.translatesAutoresizingMaskIntoConstraints = false
        $0.layer.cornerRadius = 5
    }
    //前景视图，收起时的视图
    let _foregroundView = Init(RotatedView(frame: .zero)) {
        $0.translatesAutoresizingMaskIntoConstraints = false
        $0.layer.cornerRadius = 5
    }
    let subView = MenuPersonView()
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setup()
        containerView = _containerView
        foregroundView = _foregroundView
        commonInit()
    }
    override func animationDuration(_ itemIndex: NSInteger, type: AnimationType) -> TimeInterval {
        // durations count equal it itemCount
        let durations = [0.26, 0.2, 0.2, 0.2, 0.2, 0.2] // timing animation for each view
        return durations[itemIndex]
    }
    
    func setup(){
        self.backgroundColor = UIColor.clear
        subView.backgroundColor = CuColor.colors.v2_ButtonBackgroundColor
        subView.layer.cornerRadius = 5
        self.thmemChangedHandler = { (style) -> Void in
            self._foregroundView.backgroundColor = CuColor.colors.v2_CellWhiteBackgroundColor
        }
        contentView.addSubview(_containerView)
        contentView.addSubview(_foregroundView)
        _foregroundView.addSubview(subView)
        self.itemCount = 4 //如果itemcount = 2 则cv高度必须位fv的两倍
        _containerView.snp.makeConstraints { (make) in
            make.height.equalTo(70 * itemCount)
            make.left.equalTo(6)
            make.right.equalTo(-6)
            self.containerViewTop = make.top.equalTo(3).constraint.layoutConstraints.first
        }
        _foregroundView.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView).offset(6)
            make.right.equalTo(self.contentView).offset(-6)
            self.foregroundViewTop = make.top.equalTo(self.contentView).offset(3).constraint.layoutConstraints.first
            make.height.equalTo(100) //高度和cell高度相同
        }
        subView.snp.makeConstraints { (make) in
            make.left.top.right.bottom.equalTo(_foregroundView)
        }
        _foregroundView.layoutIfNeeded()
        _containerView.layoutIfNeeded()
    }
    func bind(data: DataModel){
        _containerView.data = data
        subView.lableNum.text = String(describing: data.mangerList!.count)
        _containerView.tableView.reloadData()
    }
}
